package air2.totc.keyring;

import java.util.ArrayList;
import java.util.List;

import air2.totc.common.GestionBD;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class GestionCLES {
	// Database fields
	private SQLiteDatabase database;
	private GestionBD dbHelper;
	private String[] allColumns = { GestionBD.COLUMN_ID_CLE,GestionBD.COLUMN_ID_CONTACT,GestionBD.COLUMN_CLE,GestionBD.COLUMN_CRYPTO,GestionBD.COLUMN_COMMENTAIRE};
	public GestionCLES(Context context) {
		dbHelper = new GestionBD(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}
	// crée une objet de Type Clé avec les valeurs suivantes et l'insert dans la BDD
	public ObjectCleBDD insertCle(String cle,long Contact_id,int type_crypto,String commentaire) {
		ContentValues values = new ContentValues();
		values.put(GestionBD.COLUMN_CLE, cle);
		values.put(GestionBD.COLUMN_ID_CONTACT, Contact_id);
		values.put(GestionBD.COLUMN_CRYPTO,type_crypto);
		long insertId = database.insert(GestionBD.TABLE_TOTCCLES, null,values);
		// To show how to query
		Cursor cursor = database.query(GestionBD.TABLE_TOTCCLES,
				allColumns, GestionBD.COLUMN_ID_CLE + " = " + insertId, null,
				null, null, null);
		cursor.moveToFirst();
		return cursorToCle(cursor);
	}

	// delete L'object clé
	public void deleteCLE(ObjectCleBDD cleObject) {
		long id = cleObject.getId_cle();
		System.out.println("Comment deleted with id: " + id);
		database.delete(GestionBD.TABLE_TOTCCLES, GestionBD.COLUMN_ID_CLE
				+ " = " + id, null);
	}
	// retourne toutes les clé dans une List
	public List<ObjectCleBDD> getAllCLE() {
		List<ObjectCleBDD> listcleObject = new ArrayList<ObjectCleBDD>();
		Cursor cursor = database.query(GestionBD.TABLE_TOTCCLES,
				allColumns, null, null, null, null, null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			ObjectCleBDD cleObject = cursorToCle(cursor);
			listcleObject.add(cleObject);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return listcleObject;
	}
	
	
	// exemple (Select id_cle from TOTCCLES where type_crypt = "")
	public long[] selectIdcleWhereCryptotype(int type_crypto){
		//bouts de la requete
		String[] select = {GestionBD.COLUMN_ID_CLE};
		String where = GestionBD.COLUMN_CRYPTO +" = ?";
		String[] equal =  {""+type_crypto};
		String limit ="100";
		//resultat
		long[] listeIdcle= new long[100];
		//requete formulée
		Cursor cursor = database.query(GestionBD.TABLE_TOTCCLES,
				select, where, equal, null, null, limit);
		cursor.moveToFirst();
		int i=0;
		// parcour du cursor pour requête select
		while (!cursor.isAfterLast()) {
			listeIdcle[i]=cursor.getLong(0);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return listeIdcle;
		
	}
	// mecanisme interne pour selectionner le contenue de la requête et crée un seul object de type clé
	private ObjectCleBDD cursorToCle(Cursor cursor) {
		ObjectCleBDD cleObject = new ObjectCleBDD();
		cleObject.setId_cle(cursor.getLong(0));
		cleObject.setId_contact(cursor.getLong(1));
		cleObject.setCle(cursor.getString(2));
		cleObject.setType_cle(cursor.getInt(3));
		cleObject.setCommentaire(cursor.getString(4));
		return cleObject;
	}
}
